home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk180 / arexxtutorial / usn / usno.rexx < prev    next >
OS/2 REXX Batch file  |  1995-03-19  |  7KB  |  189 lines

  1. /* usno.rexx Dial US Naval Observatory and capture the time */
  2. /* written by Steve Poling as an excercize in AREXX programming 28 aug 89 */
  3.  
  4. /** additional modifications and tuneups by Jeremy Farrance Aug 29, 1989
  5.  ** comments inside '/** **/' are by me.  General convention used in the
  6.  ** program is that lowercase is ARexx stuff and UPPERCASE commands are
  7.  ** BaudBandit or DOS functions - this helps readability (IMHO).         **/
  8.  
  9. /** set 'failed' to false (= 0) otherwise Rexx thinks it is 'FAILED'     **/
  10. options results ; failed = 0
  11.  
  12. /* USNO sends us time in the form of a pair of lines: 
  13.  * - the first consists of an asterisk '*' on a line by itself
  14.  * - the 2nd consists of 3 groups of numbers followed by UTC in the form:
  15.  *   jjjjj ddd hhmmss UTC 
  16.  *   jjjjj stands for the 5 least significant digits of the julian date
  17.  *   ddd   Number of days since Jan 1 of this year.
  18.  *   hhmmss Hours Minutes Seconds of right now
  19.  *   UTC   Universal time (nearly same as GMT, sorta)
  20.  *
  21.  * Note that if the USNO changes their format, their phone number 
  22.  * or their baud rate, this program will have to change
  23.  */
  24.  
  25. /* this is my (sdp) first rexx program so please be gentle in criticism */
  26.  
  27. /* many times I was confronted with problems, and the only solution I could find
  28.  * was an ugly, brute force solution.  I hope that some kind soul will view this
  29.  * code with compassion, and show me a better way to do some of these things.
  30.  * I've marked such passages with a tbd: as seen below 
  31.  */
  32. /** many 'uglinesses' removed changed and modified by me, not all though **/
  33.  
  34. /* Here's one I owe JEREMY */
  35. /** and I think I owe it to someone else but I don't remember anymore **/
  36. if ~show('L',"rexxsupport.library") then do
  37.    if addlib('rexxsupport.library',0,-30,0) then
  38.       say 'added rexxsupport.library'
  39.    else do;
  40.       say 'support library not available'
  41.       exit 10
  42.       end
  43.    end
  44.  
  45. if exists('ram:USNO.tmp') then call delete('ram:USNO.tmp')
  46.  
  47. /** startup BaudBandit if its not already running **/
  48. if ~showlist('P','BAUD') then do
  49.    address command                  /** attach us to the CLI or Shell      **/
  50. /** change the below command so that we can find BaudBandit on your system
  51.  ** on my Amiga I keep BaudBandit in a directory with BB: assigned to it   **/
  52.    RUN 'GT:BaudBandit'
  53.    WAITFORPORT 'BAUD'
  54.    address                          /** swap back to REXX                  **/
  55. /** flag so we know to close BaudBandit back down later since we ran it    **/
  56.    kill_bandit = 1
  57. end
  58.  
  59. address 'BAUD'                      /* attach ourselves to baud bandit    */
  60. BAUD 1200                           /* tell baudbandit to goto 1200 baud  */
  61. SEND 'AT\r'                         /** make sure BB is paying attention **/
  62. call delay(50)                      /** do nothing for 1 second          **/
  63.  
  64. do 5                                /* make 5 attempts */
  65.    SEND '\wATDT1(202)653-0351\r'  /* USNO phone number */
  66.    failed = 0
  67.    /* wait for anything while call goes thru */
  68.    WAIT '?'
  69.    DCD                              /* is we connected? */
  70.    if rc~=0 then leave              /* leave loop as soon as connected */
  71.    else failed = 1                  /** flag a failure **/
  72. end
  73.  
  74. /** jump to done: if we failed to connect to anything **/
  75. if failed then do
  76.     say 'Unable to make telephone connection'
  77.     signal done
  78.     end
  79. /* if we're here, we're connected, slurp a couple of seconds worth of chars */
  80. CAPTURE 'ram:USNO.tmp'              /* start capture */
  81. WAIT 'UTC'
  82. WAIT 'UTC'                          /* get 2 seconds worth */
  83. call time('R')    /** start the elapsed timer so we can adjust our time    **/
  84. CAPTURE OFF
  85. SEND '\Ah'                          /** hang up on 'em **/
  86.  
  87. /** open up the file containing the text we just captured (if its there)   **/
  88. if ~open('fh','ram:USNO.tmp','R') then do
  89.     say 'Unable to open capture file'
  90.     failed = 1
  91.     signal done
  92. end /* if */                        /** 'fh' is now our file handle        **/
  93.  
  94. /** read in lines from the captured file until we get a complete USNO Time **/
  95. do until eof('fh')
  96.    failed = 0
  97.    parse value readln('fh') with julian cal_day hr_min_sec timecode .
  98.    if timecode = 'UTC' then do
  99.       say 'USNO Time:' julian cal_day hr_min_sec timecode
  100.       parse var hr_min_sec 1 hours 3 mins 5 secs +2
  101.       hours = hours - 5          /* change to 4/5 for edt/est */
  102.                                  /* change to 5/6 for cdt/cst */
  103.                                  /* change to 6/7 for mdt/mst */
  104.                                  /* change to 7/8 for pdt/pst */
  105.       day = 1
  106.       if hours < 0 then do
  107.          hours = hours + 24
  108.          day = 0
  109.       end                  
  110. /** now we get the elapsed time to adjust for the lost seconds **/
  111.       secs = secs + time('E') % 1 + 1
  112.       if secs > 59 then do
  113.          mins = mins + 1
  114.          secs = secs - 60
  115.          if mins > 59 then do
  116.             hours = hours + 1
  117.             mins = mins - 60
  118.             if hours > 23 then do
  119.                say 'Please try this again, 1 second before midnight is a pain.'
  120.                failed = 1
  121.                signal done
  122.             end
  123.          end
  124.       end
  125.       address command 'date 'hours':'mins':'secs
  126.       say 'Setting time to 'hours':'mins':'secs
  127.  
  128.       /* calculate dd-mmm-yy and tell amigados what day it is */
  129.       daymonthyear = fromjulian(julian,day)
  130.       address command 'date 'daymonthyear
  131.       say 'Setting day to 'daymonthyear
  132.  
  133.       /* this is an appropriate time to update your hardware clock */
  134.       /**   address command 'setclock save'      **/
  135.       /*    address command 'settime'              */
  136.       failed = 0
  137.       leave
  138.    end
  139.    failed = 1
  140. end
  141.  
  142. done:
  143.    call close('fh')
  144.    if exists('ram:USNO.tmp') then call delete('ram:USNO.tmp')
  145.    address command 'DATE'
  146.    say 'good night Dick'
  147.    if kill_bandit == 1 then 'SEND \AQ'
  148.    if failed then do
  149.       say 'ERROR - We failed miserably'
  150.       exit 20
  151.    end
  152.    exit 0
  153.  
  154. /* many thanx and a tip of the hat to Jeremy for moving this code to a 
  155.  * subroutine.  If you like julian date conversion stuff, bug me for the
  156.  * my upcoming release of a whole raft of astronomical programs shamelessly
  157.  * lifted from Jean Meeus' book.  Steve Poling 
  158.  */
  159. fromjulian: procedure         /* Compute dd-mmm-yy from julian */
  160. arg julian,day
  161.    julian = julian + day      /* handle midnight wrapround */
  162.    julian = julian + 2400000  /* usno truncates 2 ms digits */
  163.                               /* assume they are 24 */
  164.                               /* after 2150, this will break */
  165.                               /* contact me and I'll send you an update */
  166.  
  167. /* See pg 26 "Astronomical Formulae for Calculators", by Jean Meeus
  168.  *          4th ed. Willmann-Bell, Richmond VA 1988.                 */
  169.    /* Certain simplifying assumptions are made, mostly that you'll not
  170.     * be running this program in the past.  Also, fractional dates are
  171.     * ignored. */
  172.  
  173.    alpha = (julian-1867216.25) % 36524.25
  174.    a = julian + 1 + alpha - (alpha%4)  /* these var names are Meeus' */
  175.    b = a + 1524
  176.    c = (b - 122.1) % 365.25
  177.    d = 365.25*c; d = d % 1         /* evade AREXX arith bug */
  178.    e = (b - d) % 30.6001
  179.    day = (b - d - ((e*30.6001)%1)) % 1
  180.    if e < 13.5 then mo = e - 1
  181.    else mo = e - 13
  182.    if mo > 2.5 then year = c - 4716
  183.    else year = c - 4715
  184.    year = year // 100
  185.  
  186. return day'-'substr('JanFebMarAprMayJunJulAugSepOctNovDec',(mo-1)*3+1,3)'-'year
  187.  
  188. /** end of fromjulian() **/
  189.